<template>
<com-header title="用户登录" :back="'/home'"></com-header>
<van-form class="padded-10">
  <van-cell-group inset>
    <van-field
      v-model="mobile"
      name="用户名"
      label="用户名"
      placeholder="用户名"
      :rules="[{ required: true, message: '请填写用户名' }]"
    />
    <van-field
      v-model="password"
      type="password"
      name="密码"
      label="密码"
      placeholder="密码"
      :rules="[{ required: true, message: '请填写密码' }]"
    />
  </van-cell-group>
  
  
  <div style="margin: 16px;">
    <div class="p-t-10 p-b-10 text-blue">
        立即注册
    </div>
    <van-button round block color="#1baeae" @click="fnLogin">
      登录
    </van-button>
  </div>
</van-form>
</template>
<script lang="ts">
import { defineComponent, reactive, toRefs, getCurrentInstance } from "vue";
import { useRouter } from "vue-router";
import {fnMsg} from "@/plugins/common"
import type {ILogin} from '@/api/type'
import {login} from '@/service/index'
import md5 from 'js-md5'
import {useStore} from '@/stores/store'
export default defineComponent({
    setup() {
        const user = reactive<ILogin>({
            mobile: '',
            password: '123456'
        })
        const router = useRouter();
        const store = useStore()
        console.log(store.state.user)
        // const { proxy }: any = getCurrentInstance();//关键代码 全局挂载
        const  fnCheckData = ():boolean => {
            let mobileFormat = /^1[3|4|5|6|7|8|9][0-9]{9}$/, //手机号码格式
            passwordFormat = /^.{6,16}$/ //密码格式
            let {mobile, password} = user;
            if (!mobile) {
                fnMsg('手机号码不能为空！');
                return false;
            } else if (!mobileFormat.test(mobile)) {
                fnMsg('手机号码格式不正确！');
                return false;
            } else if (!password) {
                fnMsg('密码不能为空！');
                return false;
            } else if (!passwordFormat.test(password)) {
                fnMsg('请输入6~16位的密码');
                return false;
            } else {
                return true;
            }
        }
        const fnLogin = ():void => {
            if (fnCheckData()) {
                let {mobile, password} = user;
                login({mobile: mobile, password: md5(password).toUpperCase()}).then(res => {
                    console.log(res)
                    fnMsg(res.msg);
                    if (res.status) {
                        localStorage.setItem('login', '1');
                        let {aeskey, uid, token} = res;
                        token = {
                            t: token,
                            e: new Date().getTime()
                        }
                        store.commit('setUser', {aeskey, uid, token})
                        router.push({name: 'home'})
                    }
                })
            }
        }
        return {
            ...toRefs(user),
            fnLogin
        }
    }
})

function $toast(arg0: string) {
throw new Error("Function not implemented.");
}
</script>